package com.tiancheng.trade.commom.mq.rabbit.config;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * @Author: kellen
 * @Description: 配置信息读取
 * @Date: create in 2024/11/26 11:06
 */
@Component
@ConfigurationProperties(prefix = "mq.rabbit")
@Data
public class MyRabbitProperties {
    private List<RabbitModuleInfo> modules;

    /**
     * 根据routingKey 查询配置
     *
     * @param routingKey
     * @return
     */
    public RabbitModuleInfo getByRoutingKey(String routingKey) {
        if (!CollectionUtils.isEmpty(modules) && StringUtils.isNoneEmpty(routingKey)) {
            final Optional<RabbitModuleInfo> first = modules.stream().filter(m -> m.getRoutingKey().equals(routingKey)).findFirst();
            return first.orElse(null);
        }
        return null;
    }
}
